import {useState} from 'react'

export function useStorage(key){
    let value=localStorage.getItem(key)
    try{
        value=JSON.parse(value);
    }catch(err){
        value=value
    }
    const [state,setState]=useState(value);
    const setData=function(newData){
        setState(newData)
        if(typeof newData==='object'&&newData!==null){
            newData=JSON.stringify(newData)
        }
        localStorage.setItem(key,newData)
    }
    return [state,setData]
}